package cn.sciento.farm.devicemanager.gateway.uplink.handler;

import cn.sciento.farm.devicemanager.gateway.uplink.service.DeviceMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;

public class DeviceMessageHandler implements MessageHandler {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DeviceMessageService deviceMessageService;

    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        Object payload =  message.getPayload();
        deviceMessageService.handle(
                message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString(),
                payload
        );
    }
}

